$(function(){
	var url = common.url();
	var began = 0;
	var listlen = 10;
	var serarr = common.search();
	if(serarr.keyword){
		var serval = decodeURI(serarr.keyword);
		common.hash('page',function(){
			var jk = '/api/search/all.json?keyword='+ serval;
			fnhash(jk)
		});
	}else{
		var search = window.location.search;
		fnhash('/api/search/project.json'+search)
	}
	function fnhash(jk){
		//分页
		if(common.hashlist().page){
			began = (common.hashlist().page-1)*listlen
		}
		var url = jk +'&offset='+ began +'&limit='+ listlen
		$.ajax({
				url : url,
				type: "POST",
				error: function(request) {
					console.log(request)
				},
				dataType:"json",
				success: function(data) {
					if(data.ret){
						var json = data.data;
						var pages =Math.ceil(json.total / listlen);
						common.pagesgn(pages,6);
						$('.ser-list').html('');
						if(json.rows.length>0){
							for(var i=0;i<json.rows.length;i++){
								var title = common.maxlen(json.rows[i].title,38,true);
								var summary = common.maxlen(json.rows[i].summary,90,true);
								var times = common.timechange(json.rows[i].pubdate,true);
								if(json.rows[i].minorType == 'PROJECT'){
									var html = '<li><div class="ser-html">\
		    							<a href="/schoollist.htm?schxmid='+ json.rows[i].id +'" class="ser-title">'+ title +'</a>\
		    							<p class="ser-jj">'+ summary +'</p>\
		    							</div><div class="link-box">\
		    							<a href="/schoollist.htm?schxmid='+ json.rows[i].id +'" class="ser-link">了解详情</a></div></li>';
										$('.ser-list').append(html)
								}else{
									var html = '<li><div class="ser-html">\
		    							<a href="/detail.htm?id='+ json.rows[i].id +'" class="ser-title">'+ title +'</a>\
		    							<p class="ser-jj">'+ summary +'</p>\
		    							</div><div class="link-box">\
		    							<a href="/detail.htm?id='+ json.rows[i].id +'" class="ser-link">了解详情</a></div></li>';
										$('.ser-list').append(html)
								}
								
							}
						}else{
							$('.pages').css({'display':'none'})
						}
					}
					
					
				}
			})
	}
	// 搜索框操作
	//获取省份
	ser('/api/province/all.json',function(data){
		if(data.data.length>0){
			var sfhtml = '<option value="">-- 省份 --</option>';
			for(var i=0;i<data.data.length;i++){
				sfhtml+='<option value="'+ data.data[i].id +'">'+ data.data[i].name +'</option>'
			}
			$('select[name=provinceId]').html(sfhtml);
		}
	});
	$('select[name=provinceId]').change(function(){
		ser('/api/province/school.json?provinceId='+this.value,function(data){
			var schtml = '<option value="">-- 学校 --</option>';
			for(var i=0;i<data.data.length;i++){
				schtml+='<option value="'+ data.data[i].id +'">'+ data.data[i].name +'</option>'
			}
			$('select[name=schoolId]').html(schtml);
		})
	});
	//sousuo kasihi
	$('.proj-left .search-btn').bind('click',function(){
		//panduan checkbox
		var onok = true;
			$('.outsel').each(function(i,e){
				if(e.value == ''){
					onok = false;
					alert('省份和学校为必选项，请选择...');
					return false;
				}
			});
			
			
			if(onok){
				if($('input[name=PROJECT]').is(':checked') && !$('input[name=TENDER]').is(':checked') && !$('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=PROJECT'
				}else if(!$('input[name=PROJECT]').is(':checked') && $('input[name=TENDER]').is(':checked') && !$('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=TENDER'
				}else if(!$('input[name=PROJECT]').is(':checked') && !$('input[name=TENDER]').is(':checked') && $('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=NEWS'
				} else if($('input[name=PROJECT]').is(':checked') && $('input[name=TENDER]').is(':checked') && !$('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=PROJECT&types=TENDER'
				} else if($('input[name=PROJECT]').is(':checked') && !$('input[name=TENDER]').is(':checked') && $('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=PROJECT&types=NEWS'
				} else if(!$('input[name=PROJECT]').is(':checked') && $('input[name=TENDER]').is(':checked') && $('input[name=NEWS]').is(':checked')){
					
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=TENDER&types=NEWS'
				} else if($('input[name=PROJECT]').is(':checked') && $('input[name=TENDER]').is(':checked') && $('input[name=NEWS]').is(':checked')){
					var ukl = '/search.htm?provinceId='+$('select[name=provinceId]').val()+
					'&schoolId='+$('select[name=schoolId]').val()+'&projectName='+$('.keyword').val()+
					'&startTime='+$('input[name=startTime]').val()+'&endTime='+$('input[name=endTime]').val()+
					'&types=PROJECT&types=TENDER&types=NEWS'
				}else{
					alert('请勾选查询项...')
				}
				if($('.keyword').val() != '' || $('.keyword').val()){
					window.location.href=ukl;
				}else{
					alert('请输入查找关键字');
					return false;
				}
			}else{
			}
		
	})
	function ser(jk,fn){
		$.ajax({
			url : jk,
			type: "POST",
			error: function(request) {
				console.log(request)
			},
			dataType:"json",
			success: function(data) {
				if(!data.ret) return;
				fn?fn(data):'';
			}
		})
	}
})